#!/usr/local/bin/python -i
#==============================================================================
#
#  $Id: dopyserver,v 1.11 2002/03/09 20:09:32 mike Exp $
#
"""
   Sample DOPY server program.
   
   Usage:
      dopyserver [-dopy-thread=none|select|com|func]
   
   dopyserver publishes the "test" object, which contains some very
   basic methods for testing the protocol.
"""
#
#  Copyright (C) 1999 Michael A. Muller
#
#  Permission is granted to use, modify and redistribute this code,
#  providing that the following conditions are met:
#
#  1) This copyright/licensing notice must remain intact.
#  2) If the code is modified and redistributed, the modifications must 
#  include documentation indicating that the code has been modified.
#  3) The author(s) of this code must be indemnified and held harmless
#  against any damage resulting from the use of this code.
#
#  This code comes with ABSOLUTELY NO WARRANTEE, not even the implied 
#  warrantee of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
#
#  $Log: dopyserver,v $
#  Revision 1.11  2002/03/09 20:09:32  mike
#  Added the dopy.init() function.
#
#  Revision 1.10  2002/02/10 20:21:53  mike
#  Took out support for running from the package directory.  Added conditional
#  support for command line.
#
#  Revision 1.9  2001/08/04 19:23:24  mike
#  Added support for multiple threading modes.
#
#  Revision 1.8  2001/07/13 02:05:49  mike
#  Added two threading modes (mainly so that communication processing doesn't
#  have to consume thread resources).
#
#  Revision 1.7  1999/07/09 13:48:47  mike
#  Added tests for persistence services.
#
#  Revision 1.6  1999/06/16 14:32:22  mike
#  First cut at name services.
#
#  Revision 1.5  1999/06/11 14:03:55  mike
#  Made remote objects transportable.
#
#  Revision 1.4  1999/04/24 20:58:13  mike
#  Cleaned up for distribution.
#
#
#==============================================================================

import os, sys
import dopy
import tcp

class Test:
   
   def hello(self, str):
      print 'hello', str, \
         'received from', hub.getContext().protocol().getProtocolId()
      return 'hello ' + str

   def kwparms(self, p1, p2):
      print 'kwparms =', p1, p2, \
         'received from', hub.getContext().protocol().getProtocolId()
      return (p1, p2)
   
   def raiseException(self):
      print 'raiseException received from', \
         hub.getContext().protocol().getProtocolId()
      raise Exception("Something bad happened")

   def getTransient(self):
      print 'getTransient received from', \
         hub.getContext().protocol().getProtocolId()
      return hub.makeAnonObject(Transient())

   def testCallback(self, remote, text):
      print 'testCallback received from', \
         hub.getContext().protocol().getProtocolId()
      callbackValue = remote.doit(text)
      print 'got return value from callback:', callbackValue
      return callbackValue

class Transient:
   """
      This class is a test of a transient object which will be created
      and registered anonymously.
   """
   def hello(self, str):
      print 'transient hello', str, \
         'received from', hub.getContext().protocol().getProtocolId()
      return 'hello ' + str

# init the system, passing args to DOPY
dopy.init(sys.argv)

hub = dopy.getHub()
tcp.makeServer(9600)
hub.addObject('test', Test())

# on posix systems, give a little command line
if os.name == 'posix':
   import minicl
   hub.addReactor(minicl.MiniComLine())

# add a naming context - making this a name server!!
from naming import StandardNamingContext
namingContext = StandardNamingContext()
hub.addObject('naming', namingContext)

# add persistant object services
from pos import FileSysDirectory
hub.addObject('pos', FileSysDirectory('.', 'pos'))

hub.mainloop()
